package edu.hz.dao;

import edu.hz.model.dto.HomeTopContributingDonationDto;
import edu.hz.model.dto.HomeTopDonationDto;
import edu.hz.model.entity.DonationEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DonationMapper {

    @Select("select * from donations where id = #{id}")
    DonationEntity selectById(@Param("id") Integer id);

    @Select("select * from donations")
    List<DonationEntity> allDonations();

    @Insert("insert into donations(name, start, end, need_money, current_money, author, profile, content, is_top)" +
            "values (#{name}, #{start}, #{end}, #{needMoney}, #{currentMoney}, #{author}, #{profile}, #{content}, #{isTop})")
    int insertDonation(DonationEntity donationEntity);

    @Select("select * from donations order by id desc limit #{count}")
    List<DonationEntity> lastedDonations(@Param("count") Integer count);

    @Select("select d.id, d.name, d.author, d.send_time, COUNT(ud.id) as donationCount "
    + "from donations d join user_donations ud "
            + "on d.id = ud.donation_id "
            + "where d.start < NOW() and d.end > NOW() "
            + "group by d.id, d.name, d.author, d.send_time "
            + "order by donationCount DESC "
            + "limit #{count}"
    )
    List<HomeTopContributingDonationDto> topContributingProjects(@Param("count") Integer count);


    @Select("select * from donations order by current_money desc limit #{count}")
    List<DonationEntity> mostMoney(@Param("count") Integer count);


    @Select("select d.id, d.name, d.profile, d.current_money as currentMoney, d.send_time as sendTime, COUNT(ud.id)"
            + " from donations d left join user_donations ud on d.id = ud.donation_id "
    + "where d.is_top = 1 "
    + "group by d.id, d.name, d.author, d.profile, d.current_money, d.send_time "
    + "order by d.send_time desc "
    + "limit #{count}")
    List<HomeTopDonationDto> topDonations(@Param("count") Integer count);


}
